package io.finer.erp.jeecg.purchase.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.finer.erp.jeecg.purchase.entity.PurchaseAddress;
import io.finer.erp.jeecg.purchase.service.PurchaseAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * <p>
 * 地址信息 前端控制器
 * </p>
 *
 * @author yaoyifan
 * @since 2021-06-04
 */
@Api(tags = "采购配置地址信息")
@RestController
@RequestMapping("/purchase/purchaseAddress")
@Slf4j
public class PurchaseAddressController {
    
    @Autowired
    PurchaseAddressService purchaseAddressService;
    
    @AutoLog(value="地址信息-分页列表查询")
    @ApiOperation(value="地址信息-分页列表查询", notes = "地址信息-分页列表查询")
    @GetMapping("/list")
    public Result<?> queryPageList(PurchaseAddress purchaseAddress,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req){
        QueryWrapper<PurchaseAddress> queryWrapper = QueryGenerator.initQueryWrapper(purchaseAddress, req.getParameterMap());
        Page<PurchaseAddress> page = new Page<>(pageNo, pageSize);
        IPage<PurchaseAddress> pageList = purchaseAddressService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    @AutoLog(value = "地址信息-添加")
    @ApiOperation(value="地址信息-添加", notes="地址信息-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody PurchaseAddress purchaseAddress) {
        purchaseAddressService.save(purchaseAddress);
        return Result.ok("添加成功！");
    }

    @AutoLog(value = "地址信息-编辑")
    @ApiOperation(value="地址信息-编辑", notes="地址信息-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody PurchaseAddress purchaseAddress) {
        PurchaseAddress purchaseAddressEntity = purchaseAddressService.getById(purchaseAddress.getId());
        if(purchaseAddressEntity==null) {
            return Result.error("未找到对应数据");
        }
        purchaseAddressService.updateById(purchaseAddressEntity);
        return Result.ok("编辑成功!");
    }

    @AutoLog(value = "地址信息-删除")
    @ApiOperation(value="地址信息-删除", notes="地址信息-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id", required = true) String id){
        this.purchaseAddressService.removeById(id);
        return Result.ok("删除成功!");
    }


    @AutoLog(value = "地址信息-批量删除")
    @ApiOperation(value="地址信息-批量删除", notes="地址信息-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        this.purchaseAddressService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功！");
    }

    @AutoLog(value = "地址信息-通过id查询")
    @ApiOperation(value="地址信息-通过id查询", notes="地址信息-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        PurchaseAddress purchaseAddress = purchaseAddressService.getById(id);
        if(purchaseAddress==null) {
            return Result.error("未找到对应数据");
        }
        return Result.ok(purchaseAddress);
    }

}

